extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
}
#include <iostream>

class h264_dec
{
  private:
	// NOTE: this pointer must be set to NULL before trying to call avformat_open_input
	AVFormatContext *if_ctx = NULL;
	AVCodec *av_codec;
	AVCodecContext *av_codec_ctx;
	int vstream_no;
	int prepared = 0;
	AVPacket *packet;
	AVFrame *av_frame, *rgb_frame;
	SwsContext *sws_ctx;
  public:
	h264_dec();

	~h264_dec();

	/**
	 * @brief close rtsp stream and free all data structs
	 *
	 * @return int always 0
	 */
	int close_stream();

	/**
	 * @brief open rtsp stream and prepare a decoder for the stream
	 *
	 * @param rtsp_url rtsp stream url
	 *
	 * @return int 0 on success, <0 if failed
	 */
	int open_stream(std::string input_url, std::string decoder, std::string scale_size);


	/**
	 * @brief decode a frame, copy image data and size
	 * 
	 * @param data data buffer for decoded frame
	 * @param len lenght of data buffer, should be greater or equal than height * step
	 * @param width image width, may be smaller than step
	 * @param height image height, how many rows does the image have
	 * @param step size of each row
	 * @return int 0: successful, <0: failed or eof
	 */
	int decode_frame(uint8_t *data, int len, int *width, int *height, int *step);
};


